package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysOssConfigDo;
import com.ruoyi.system.mapper.SysOssConfigMapper;
import com.ruoyi.system.service.ISysOssConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 对象存储配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysOssConfigServiceImpl implements ISysOssConfigService {
    private final SysOssConfigMapper sysOssConfigMapper;

    /**
     * 查询对象存储配置
     */
    @Override
    public SysOssConfigDo queryById(Long ossConfigId) {
        return sysOssConfigMapper.selectById(ossConfigId);
    }

    /**
     * 查询对象存储配置列表
     */
    @Override
    public TableDataInfo<SysOssConfigDo> queryPageList(SysOssConfigDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOssConfigDo> lqw = buildQueryWrapper(entity);
        Page<SysOssConfigDo> result = sysOssConfigMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询对象存储配置列表
     */
    @Override
    public List<SysOssConfigDo> queryList(SysOssConfigDo entity) {
        LambdaQueryWrapper<SysOssConfigDo> lqw = buildQueryWrapper(entity);
        return sysOssConfigMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysOssConfigDo> buildQueryWrapper(SysOssConfigDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysOssConfigDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getConfigKey()), SysOssConfigDo::getConfigKey, entity.getConfigKey());
        lqw.eq(StringUtils.isNotBlank(entity.getAccessKey()), SysOssConfigDo::getAccessKey, entity.getAccessKey());
        lqw.eq(StringUtils.isNotBlank(entity.getSecretKey()), SysOssConfigDo::getSecretKey, entity.getSecretKey());
        lqw.like(StringUtils.isNotBlank(entity.getBucketName()), SysOssConfigDo::getBucketName, entity.getBucketName());
        lqw.eq(StringUtils.isNotBlank(entity.getPrefix()), SysOssConfigDo::getPrefix, entity.getPrefix());
        lqw.eq(StringUtils.isNotBlank(entity.getEndpoint()), SysOssConfigDo::getEndpoint, entity.getEndpoint());
        lqw.eq(StringUtils.isNotBlank(entity.getDomain()), SysOssConfigDo::getDomain, entity.getDomain());
        lqw.eq(StringUtils.isNotBlank(entity.getIsHttps()), SysOssConfigDo::getIsHttps, entity.getIsHttps());
        lqw.eq(StringUtils.isNotBlank(entity.getRegion()), SysOssConfigDo::getRegion, entity.getRegion());
        lqw.eq(StringUtils.isNotBlank(entity.getAccessPolicy()), SysOssConfigDo::getAccessPolicy, entity.getAccessPolicy());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysOssConfigDo::getStatus, entity.getStatus());
        lqw.eq(StringUtils.isNotBlank(entity.getExt1()), SysOssConfigDo::getExt1, entity.getExt1());
        return lqw;
    }

    /**
     * 新增对象存储配置
     */
    @Override
    public Boolean insert(SysOssConfigDo entity) {
        SysOssConfigDo add = BeanUtil.toBean(entity, SysOssConfigDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysOssConfigMapper.insert(add) > 0;
        if (flag) {
            entity.setOssConfigId(add.getOssConfigId());
        }
        return flag;
    }

    /**
     * 修改对象存储配置
     */
    @Override
    public Boolean updateById(SysOssConfigDo entity) {
        SysOssConfigDo update = BeanUtil.toBean(entity, SysOssConfigDo.class);
        validEntityBeforeSave(update);
        return sysOssConfigMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOssConfigDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除对象存储配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysOssConfigMapper.deleteBatchIds(ids) > 0;
    }
}
